<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAudit.visible"
    :width="900"
    :title="dialogAudit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-row justify="center" :gutter="20">
      <el-col :span="20" :offset="0">
        <el-descriptions :column="3">
          <el-descriptions-item label="企业名称">{{ detailInfo.name }}</el-descriptions-item>
          <el-descriptions-item label="企业信用代码">{{ detailInfo.code }}</el-descriptions-item>
          <el-descriptions-item label="经济类型">{{ detailInfo.economicTypeName }}</el-descriptions-item>
          <el-descriptions-item label="单位类型">{{ detailInfo.typeName }}</el-descriptions-item>
          <el-descriptions-item label="注册资本">{{ detailInfo.regCapital }}</el-descriptions-item>
          <el-descriptions-item label="法人姓名">{{ detailInfo.legalman }}</el-descriptions-item>
          <el-descriptions-item label="法人手机号码">{{ detailInfo.legalMobile }}</el-descriptions-item>
          <el-descriptions-item label="法人职务">{{ detailInfo.legalDuty }}</el-descriptions-item>
          <el-descriptions-item label="法人职称">{{ detailInfo.legalTitle }}</el-descriptions-item>
          <el-descriptions-item label="归属区域">{{ detailInfo.areaName }}</el-descriptions-item>
          <el-descriptions-item label="邮编">{{ detailInfo.zipCode }}</el-descriptions-item>
          <el-descriptions-item label="企业注册日期">{{ detailInfo.registerDate }}</el-descriptions-item>
          <el-descriptions-item label="注册地址">{{ detailInfo.regAddr }}</el-descriptions-item>
          <el-descriptions-item label="办公地址">{{ detailInfo.officeAddr }}</el-descriptions-item>
          <el-descriptions-item label="联系人">{{ detailInfo.linkMan }}</el-descriptions-item>
          <el-descriptions-item label="联系人电话">{{ detailInfo.linkTel }}</el-descriptions-item>
          <el-descriptions-item label="企业备注">{{ detailInfo.remark }}</el-descriptions-item>
          <el-descriptions-item label="数据来源">{{ detailInfo.dataSourceName }}</el-descriptions-item>
          <el-descriptions-item label="营业执照" span="3">
            <span
              v-for="item in detailInfo.attachments"
              :key="item.attachmentId"
              style="padding-right: 10px"
              @click="download(item.attachmentId)"
            >
              <el-link type="primary">{{ item.attachmentName }}</el-link>
            </span></el-descriptions-item
          >
        </el-descriptions></el-col
      >
      <el-col :span="20" :offset="0">
        <el-form ref="auditFormRef" :model="auditFormData" label-width="80" :rules="auditRules">
          <el-form-item label="审核意见" prop="auditIdea" style="margin-bottom: 18px">
            <el-checkbox
              v-model="yesChecked"
              label="同意"
              @change="
                (value: boolean) => {
                  if (value) {
                    auditFormData.auditIdea = 1;
                    noChecked = false;
                  } else auditFormData.auditIdea = undefined;
                }
              "
            />
            <el-checkbox
              v-model="noChecked"
              label="不同意"
              @change="
                (value: boolean) => {
                  if (value) {
                    auditFormData.auditIdea = -1;
                    yesChecked = false;
                  } else auditFormData.auditIdea = undefined;
                }
              "
            />
          </el-form-item>
          <el-form-item label="" prop="auditIdeaContent" style="margin-bottom: 18px">
            <el-input v-model="auditFormData.auditIdeaContent" type="textarea" :rows="3"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAudit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { AuditForm } from "@/api/system/corps/types";
import { getCorpsInfo, AuditCorp } from "@/api/system/corps/index";
import { FormRules } from "element-plus";
import type { UploadUserFile } from "element-plus";
import { downloadFile } from "@/utils";
import { downloadAttachment } from "@/api/common/other/index";

const props = defineProps({
  dialogAudit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "企业信息审核",
      footBtn: "确认提交",
      id: 0,
    }),
  },
});

const { dialogAudit } = toRefs(props);

const emit = defineEmits(["sendClose"]);

const yesChecked = ref(false);
const noChecked = ref(false);
const loading = ref(false);

const detailInfo = ref<any>({});
const auditFormData = reactive<AuditForm>({});
const auditFormRef = ref(ElForm);
//上传文件列表
const fileList = ref<UploadUserFile[]>([]);

const auditRules = reactive<FormRules<AuditForm>>({
  auditIdea: [{ required: true, message: "请填写审核意见", trigger: "blur" }],
  auditIdeaContent: [{ required: true, message: "请选择审核意见", trigger: "blur" }],
});

//  下载附件
const download = (id: string) => {
  downloadAttachment(id).then((res) => {
    downloadFile(res);
  });
};

const initForm = () => {
  if (dialogAudit.value.visible) {
    loading.value = true;
    getCorpsInfo(dialogAudit.value.id)
      .then()
      .then((res) => {
        if (res.success) {
          detailInfo.value = res.data;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }
};

onMounted(() => {
  initForm();
});

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean) {
  emit("sendClose", query);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  auditFormRef.value.resetFields();
  auditFormRef.value.clearValidate();
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  auditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      console.log(fileList);
      AuditCorp(dialogAudit.value.id, auditFormData)
        .then((res) => {
          if (res.success) {
            closeDialog(true);
          } else {
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
